home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / news / nntp / nntplink3.1.0 / parseargs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-25  |  9.7 KB  |  398 lines

  1. #include "conf.h"
  2. #include <errno.h>
  3. #include <sys/stat.h>
  4. #ifdef HAVE_STRING_H
  5. #include <string.h>
  6. #else
  7. #include <strings.h>
  8. #endif
  9. #ifdef FAKESYSLOG
  10. #include "fsyslog.h"
  11. #else
  12. #include <syslog.h>
  13. #endif
  14. #include "readline.h"
  15. #include "nntplink.h"
  16. #include "strfuns.h"
  17.  
  18. extern int Clear_batchfile;
  19. extern int Close_after;
  20. extern long Close_timeout;
  21. extern long Entry_sleep;
  22. extern long Exit_timeout;
  23. extern long Fail_minutes;
  24. extern int Log_after;
  25. extern Boolean Log_close;
  26. extern Boolean Open_art_first;
  27. extern char *Prog_name;
  28. extern long Repeat_minutes;
  29. extern Boolean Report_stats;
  30.  
  31. extern char *E_chdir;
  32. extern char *E_fopen;
  33.  
  34. extern char *optarg;
  35. extern int optind, opterr;
  36.  
  37. extern void exit();
  38. extern void fail();
  39. extern void log();
  40.  
  41. Boolean Autobackground = FALSE;
  42. Boolean Debug = FALSE;
  43. FILE *Debugfp = stderr;
  44. char *History_file = HISTORYFILE;
  45. int Input_from = DEF_INPUT_FROM;
  46. Boolean One_shot = FALSE;
  47. Boolean Save_fails = TRUE;
  48. int Nntp_port = 0;
  49. #ifdef XREPLIC
  50. int Xreplic = FALSE;
  51. #endif
  52.  
  53. void
  54.   parse_args(argc, argv)
  55. int argc;
  56. char *argv[];
  57. {
  58.     static char *fname = "parse_args: ";
  59.     int c, errflg=0, t;
  60.     char *batchname = NULL,
  61.          *tfile = NULL,
  62.          *batchdir = BATCHDIR,
  63.          *spooldir = SPOOLDIR;
  64.     Boolean set_C_opt = FALSE;
  65.     Boolean c_news_batch = FALSE;
  66.     Boolean print_version = FALSE;
  67.     struct stat qst;
  68.  
  69.     if ((Prog_name = strrchr(argv[0], '/')) != NULL)
  70.       Prog_name++;
  71.     else
  72.       Prog_name = argv[0];
  73.  
  74.     opterr = 0;
  75.     while ((c = getopt(argc, argv,
  76.                "a:Ab:B:c:C:dD:e:E:fF:H:i:l:Lm:n:N:oOp:P:rR:s:S:vx"))
  77.        != EOF)
  78.         switch (c) {
  79.         case 'a':
  80.             Close_after = atoi(optarg);
  81.         break;
  82.         case 'A':
  83.         Autobackground = TRUE;
  84.         break;
  85.         case 'b':
  86.         batchname = optarg;
  87.         break;
  88.         case 'B':
  89.         batchdir = optarg;
  90.         break;
  91.         case 'c':
  92.         Clear_batchfile = atoi(optarg);
  93.         set_C_opt = TRUE;
  94.         break;
  95.         case 'C':
  96.         Close_timeout = atol(optarg);
  97.         break;
  98.         case 'd':
  99.         Debug = TRUE;
  100.         break;
  101.         case 'D':
  102.         Debug = TRUE;
  103.         if ((Debugfp = fopen(optarg, "a")) == NULL) {
  104.             fprintf(stderr, E_fopen, optarg, "a", errmsg(errno));
  105.             errflg++;
  106.         }
  107.         break;
  108.         case 'e':
  109.         Exit_timeout = atol(optarg);
  110.         break;
  111.         case 'E':
  112.         Entry_sleep = atol(optarg);
  113.         break;
  114.         case 'f':
  115.         Save_fails = FALSE;
  116.         break;
  117.         case 'F':
  118.         Input_from = FLG_LOGFILE;
  119.         Logfile.name = optarg;
  120.         break;
  121.         case 'H':
  122.         History_file = optarg;
  123.         break;
  124.         case 'i':
  125.         if (strcmp(optarg, "batchfile") == MATCH)
  126.           Input_from = FLG_BATCHFILE;
  127.         else if (strcmp(optarg, "logfile") == MATCH)
  128.           Input_from = FLG_LOGFILE;
  129.         else if (strcmp(optarg, "stdin") == MATCH)
  130.           Input_from = FLG_STDIN;
  131.         else
  132.           errflg++;
  133.         break;
  134.         case 'l':
  135.         Log_after = atoi(optarg);
  136.         break;
  137.         case 'L':
  138.         Log_close = TRUE;
  139.         break;
  140.         case 'm':
  141.         Fail_minutes = atol(optarg);
  142.         break;
  143.         case 'n':
  144.         Batchfile.nap_time = atol(optarg);
  145.         break;
  146.         case 'N':
  147.         Logfile.nap_time = atol(optarg);
  148.         break;
  149.         case 'o':
  150.         One_shot = TRUE;
  151.         break;
  152.         case 'O':
  153.         Open_art_first = TRUE;
  154.         break;
  155.         case 'p':
  156.         if (strcmp(optarg, "decnet") == 0)
  157.           Host.transport = T_DECNET;
  158.         else if (strcmp(optarg, "tcp") == 0)
  159.           Host.transport = T_IP_TCP;
  160.         else if (strcmp(optarg, "dkhost") == 0)
  161.           Host.transport = T_DKHOST;
  162.         else
  163.           errflg++;
  164.         break;
  165.         case 'P':
  166.         Nntp_port = atoi(optarg);
  167.         break;
  168.         case 'r':
  169.         Report_stats = FALSE;
  170.         break;
  171.         case 'R':
  172.         Repeat_minutes = atol(optarg);
  173.         break;
  174.         case 's':
  175.         Input_from = FLG_LOGFILE;
  176.         Host.sysname = optarg;
  177.         break;
  178.         case 'S':
  179.         spooldir = optarg;
  180.         break;
  181.         case 'v':
  182.         print_version = TRUE;
  183.         Debug = TRUE;
  184.         break;
  185.         case 'x':
  186. #ifdef XREPLIC
  187.         Xreplic = TRUE;
  188. #else
  189.         fprintf(stderr, "%s: Xreplic not compiled in\n", Prog_name);
  190.         errflg++;
  191. #endif
  192.         break;
  193.         default:
  194.         errflg++;
  195.         }
  196.  
  197.     if (optind < argc) {
  198.     Host.name = argv[optind++];
  199.     } else {
  200.     if (print_version)
  201.       Host.name = strsave("<none-given>");
  202.     errflg++;
  203.     }
  204.  
  205. #ifdef XREPLIC
  206.     if (Xreplic && (Input_from & FLG_LOGFILE)) {
  207.     fprintf(stderr, "%s: Xreplic cannot be used with the LOGFILE mode\n",
  208.         Prog_name);
  209.     errflg++;
  210.     }
  211. #endif
  212.  
  213.     if (errflg) {
  214.     fprintf(stderr,    "usage: %s [options] hostname\n", Prog_name);
  215.     if (!print_version)
  216.       exit(FAIL);
  217.     }
  218.  
  219.     if (Autobackground && (Debug || One_shot)) {
  220.     fprintf(stderr, "%s: Cannot use -A and %s\n", Prog_name,
  221.         Debug ? "-d" : "-o");
  222.     exit(FAIL);
  223.     }
  224.  
  225.     /*
  226.      * If our umask is 0, set it to 2, otherwise set it back to what it was.
  227.      */
  228.  
  229.     if ((t = umask(2)) != 0)
  230.       umask(t);
  231.  
  232.     if ((t = Clear_batchfile / Log_after) < 1)
  233.       t = 1;
  234.  
  235.     if ((Clear_batchfile != (t * Log_after)) && (set_C_opt)) {
  236.     fprintf(stderr,
  237.           "%s: -c (Clear_batchfile) isn't a multiple of -l (Log_after)\n",
  238.           Prog_name);
  239.     fprintf(stderr,
  240.           "     using %d for -c instead\n", (t * Log_after));
  241.     }
  242.     Clear_batchfile = t;
  243.  
  244.     if (chdir(spooldir) == FAIL)
  245.       log(LOG_WARNING, fname, E_chdir, Host.name, spooldir, errmsg(errno));
  246.  
  247.     if (Host.sysname == NULL)
  248.     Host.sysname = Host.name;
  249.  
  250.     if (batchname == NULL)
  251.       tfile = str3save(batchdir, "/", Host.name);
  252.     else if (*batchname == '/')
  253.       tfile = strsave(batchname);
  254.     else
  255.       tfile = str3save(batchdir, "/", batchname);
  256.  
  257.     if (stat(tfile, &qst) == FAIL) {
  258.  
  259.     if (errno == ENOENT)
  260.       dlog(LOG_INFO, fname,
  261.            "%s%s doesn't exist, assuming it's the name of a batchfile\n",
  262.            tfile);
  263.     else
  264.       fail(fname, "%s%s: stat(%s) failed: %s\n", Host.name, tfile,
  265.            errmsg(errno));
  266.     } else if ((qst.st_mode & S_IFMT) == S_IFREG) {
  267.     dlog(LOG_DEBUG, fname, "%s%s is a batchfile\n", tfile);
  268.     } else if ((qst.st_mode & S_IFMT) == S_IFDIR) {
  269.     dlog(LOG_DEBUG, fname, "%s%s is a batch directory\n", tfile);
  270.     c_news_batch = TRUE;
  271.     } else {
  272.     fail(fname,
  273.          "%s%s: %s is not a directory or a regular file, st_mode = %o",
  274.          Host.name, tfile, qst.st_mode);
  275.     }
  276.  
  277.     if (c_news_batch) {
  278.     Batchfile.name = str2save(tfile, "/togo");
  279.     Host.failfile = str2save(tfile, "/togo.fail");
  280.     Host.datafile = str2save(tfile, "/togo.link");
  281.     } else {
  282.         Batchfile.name = strsave(tfile);
  283.     Host.failfile = str2save(tfile, ".fail");
  284.     Host.datafile = str2save(tfile, ".link");
  285.     }
  286.  
  287.     FREE(tfile);
  288.  
  289.     Batchfile.nname = str2save(Batchfile.name, ".nntp");
  290.     Batchfile.tmp = str2save(Batchfile.name, ".tmp");
  291.  
  292.     if (Debug) {
  293.     log(LOG_DEBUG, "",
  294.         "%sConfiguration information:\n");
  295.     log(LOG_DEBUG, "",
  296.         "%s Version:                         %s\n", VERSION);
  297.     log(LOG_DEBUG, "",
  298.         "%s Host:                            %s\n", Host.name);
  299.     log(LOG_DEBUG, "",
  300.         "%s Spool directory:                 %s\n", spooldir);
  301.     log(LOG_DEBUG, "",
  302.         "%s Batch file:                      %s\n", Batchfile.name);
  303.     log(LOG_DEBUG, "",
  304.         "%s Fail file:                       %s\n", Host.failfile);
  305.     if (!One_shot) {
  306.         log(LOG_DEBUG, "",
  307.         "%s Link Data file:                  %s\n", Host.datafile);
  308.         if (Input_from & FLG_LOGFILE) {
  309.         log(LOG_DEBUG, "",
  310.             "%s Logfile:                         %s\n", Logfile.name);
  311.         log(LOG_DEBUG, "",
  312.             "%s Sysname:                         %s\n", Host.sysname);
  313.         }
  314. #ifdef LOOKUP_ARTICLE
  315.         log(LOG_DEBUG, "",
  316.         "%s History file:                    %s\n", History_file);
  317. #endif
  318.     } else
  319.       log(LOG_DEBUG, "", "%s One shot?:                       TRUE\n");
  320.     if (Host.transport == T_IP_TCP)
  321.       log(LOG_DEBUG, "",
  322.           "%s Connection Type:                 TCP/IP\n");
  323.     else if (Host.transport == T_DECNET)
  324.       log(LOG_DEBUG, "",
  325.           "%s Connection Type:                 DECNET\n");
  326.     else if (Host.transport == T_DKHOST)
  327.       log(LOG_DEBUG, "",
  328.           "%s Connection Type:                 DKHOST\n");
  329.     else
  330.       log(LOG_DEBUG, "",
  331.           "%s Connection Type:                 <fd>\n");
  332.     if (Input_from & FLG_BATCHFILE)
  333.       log(LOG_DEBUG, "",
  334.           "%s Input file type:                 Batchfile\n");
  335.     else if (Input_from & FLG_LOGFILE)
  336.       log(LOG_DEBUG, "",
  337.           "%s Input file type:                 Logfile\n");
  338.     else
  339.       log(LOG_DEBUG, "",
  340.           "%s Input file type:                 <stdin>\n");
  341.     log(LOG_DEBUG, "",
  342.         "%s Save fails:                      %s\n",
  343.         (Save_fails ? "TRUE" : "FALSE"));
  344.     log(LOG_DEBUG, "",
  345.         "%s Fail_minutes:                    %ld\n", Fail_minutes);
  346.     log(LOG_DEBUG, "",
  347.         "%s Repeat_minutes:                  %ld\n", Repeat_minutes);
  348.     if (Input_from & FLG_LOGFILE)
  349.       log(LOG_DEBUG, "",
  350.           "%s Sleep between logfile checks:    %d second(s)\n",
  351.           Logfile.nap_time);
  352.     else
  353.       log(LOG_DEBUG, "",
  354.           "%s Sleep between batchfile checks:  %d second(s)\n",
  355.           Batchfile.nap_time);
  356.     log(LOG_DEBUG, "",
  357.         "%s Sleep between entry checks:      %d second(s)\n",
  358.         Entry_sleep);
  359.     if (Input_from & FLG_BATCHFILE)
  360.       log(LOG_DEBUG, "",
  361.           "%s Batchfile is cleared after:      %d article(s)\n",
  362.           (Clear_batchfile * Log_after));
  363.     log(LOG_DEBUG, "",
  364.         "%s Statistics Reported:             %s\n",
  365.         (Report_stats ? "TRUE" : "FALSE"));
  366.     if (Report_stats) {
  367. #ifdef FAKESYSLOG
  368.         log(LOG_DEBUG, "",
  369.         "%s                 Via:             %s\n", FAKESYSLOG);
  370. #else
  371.         log(LOG_DEBUG, "",
  372.         "%s                 Via:             4.3BSD syslog\n");
  373. #endif
  374.         log(LOG_DEBUG, "",
  375.         "%s               After:             %d article(s)\n",
  376.         Log_after);
  377.         if (Log_close)
  378.           log(LOG_DEBUG, "",
  379.          "%s               and each time the connection is closed.\n");
  380.     }
  381.     log(LOG_DEBUG, "",
  382.         "%s Connection closed after:         %d article(s)\n",
  383.         Close_after);
  384.     if (Close_timeout >= 0)
  385.       log(LOG_DEBUG, "",
  386.           "%s Connection closed after:         %d idle second(s)\n",
  387.           Close_timeout);
  388.     if (Exit_timeout >= 0)
  389.       log(LOG_DEBUG, "",
  390.           "%s Exit after:                      %d idle second(s)\n",
  391.           Exit_timeout);
  392.     log(LOG_DEBUG, "", "\n\n");
  393.     }
  394.     if (print_version)
  395.       exit(0);
  396.     return;
  397. }
  398.